package com.steshov.trainer.activities;

import static com.steshov.trainer.utils.App.CREATE_TASK_MODE;
import static com.steshov.trainer.utils.App.EDIT_TASK_MODE;
import static com.steshov.trainer.utils.App.taskMode;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.steshov.trainer.R;
import com.steshov.trainer.dao.TaskDAO;
import com.steshov.trainer.model.Task;
import com.steshov.trainer.utils.Adapters;
import com.steshov.trainer.utils.App;

public class TaskActivity extends Activity {

	Button toExecisesButton, toMainButton, createTaskButton;
	TaskDAO taskDAO = new TaskDAO();
	Spinner spinner;
	EditText amountField;
	DatePicker datePicker;
	TimePicker timePicker;
	TextView title;
	static Task editingTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_task);
		spinner = (Spinner) findViewById(R.id.spinner1);
		amountField = (EditText) findViewById(R.id.editText1);
		datePicker = (DatePicker) findViewById(R.id.datePicker1);
		timePicker = (TimePicker) findViewById(R.id.timePicker1);
		title = (TextView) findViewById(R.id.textView1);
		toExecisesButton = (Button) findViewById(R.id.button3);
		toMainButton = (Button) findViewById(R.id.button2);
		toExecisesButton.setOnClickListener(Adapters.toExercisePage());
		toMainButton.setOnClickListener(Adapters.toMainPage());
		App.taskMenuMode = taskMode;
		initSpinner();
		switch (taskMode) {
		case CREATE_TASK_MODE:
			initInCreateMode();
			break;
		case EDIT_TASK_MODE:
			App.taskMode = App.CREATE_TASK_MODE;
			initInEditMode();
			break;
		default:
			break;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		if (App.taskMenuMode == CREATE_TASK_MODE) {
			getMenuInflater().inflate(R.menu.activity_index, menu);
			App.initOptMenu(menu);
		}
		return true;
	}

	private void initSpinner() {
		String[] spinnerArray = taskDAO.getExercises(TaskActivity.this);
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, spinnerArray);
		spinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerArrayAdapter);
	}

	private void initInEditMode() {
		int itemPosition = ((ArrayAdapter<String>) spinner.getAdapter())
				.getPosition(editingTask.getExercise());
		toMainButton.setVisibility(View.INVISIBLE);
		toExecisesButton.setVisibility(View.INVISIBLE);
		title.setText("Update task");
		Button editTaskButton = (Button) findViewById(R.id.button1);
		editTaskButton.setText("Update");
		amountField.setText(String.valueOf(editingTask.getAmount()));
		spinner.setSelection(itemPosition);

		Date d = new Date(editingTask.getTime());
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);

		datePicker.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH));
		timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(cal.get(Calendar.MINUTE));
		editTaskButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String exercise = spinner.getSelectedItem().toString();
				int amount = Integer.parseInt(amountField.getText().toString());
				Long time = App.getMiliseconds(datePicker, timePicker);
				taskDAO.updateTask(TaskActivity.this,
						new Task(editingTask.getId(), exercise, amount, time));
				TaskActivity.this.finish();
			}
		});

	}

	private void initInCreateMode() {
		title.setText("Create task");
		createTaskButton = (Button) findViewById(R.id.button1);
		createTaskButton.setText("Create");
		toMainButton.setVisibility(View.VISIBLE);
		toExecisesButton.setVisibility(View.VISIBLE);
		createTaskButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!amountField.getText().toString().equals("")) {
					String exercise = spinner.getSelectedItem().toString();
					int amount = Integer.parseInt(amountField.getText()
							.toString());
					Long time = App.getMiliseconds(datePicker, timePicker);
					taskDAO.peristTask(TaskActivity.this, new Task(null,
							exercise, amount, time),true);
				} else {
					App.msg(TaskActivity.this, "Enter amount of exercises");
				}
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		App.onOptionsItemSelectedThing(item, this);
		return super.onOptionsItemSelected(item);
	}
}
